﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPMS.Commons.Configuration;
using SPMS.Log.Core;
using SPMS.Utility;

namespace SPMS.Log.Configuration.Handler
{
    /// <summary>
    /// 日志过滤器节点处理器
    /// </summary>
    /// <remarks>用于处理日志过滤器节点</remarks>
    /// <history>
    /// [zanezeng]               2009/10/15 14:44    创建
    /// </history>
    public sealed class Filter_NodeHandler : AbstractNodeHandler
    {
        /// <summary>
        /// 处理指定的节点
        /// </summary>
        /// <param name="node">配置节点</param>
        /// <param name="context">处理上下文</param>
        /// <returns>根据配置信息创建的对象</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-11-17 11:39    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/15 14:44    创建
        /// </history>
        protected override object ProcessInternal( IConfigurationNode node, ParseContext context )
        {
            //输出日志
            if (LoggingContext.MetaLog.IsTraceEnabled)
            {
                LoggingContext.MetaLog.Trace( "开始解析日志过滤器" );
            }

            //获得日志类型
            string typeName = this.GetAttributeValue( node, LogConfiguationTags.ATTRIBUTE_TYPE, context );

            //判断是否指定了追加器的类型
            if (string.IsNullOrEmpty( typeName ))
            {
                //抛出异常
                throw new ConfigurationException( string.Format( Properties.Resources.MSG_CONFIGURATION_ATTRIBUTE_REQUIRED, node.Name, LogConfiguationTags.ATTRIBUTE_TYPE ), node );
            }

            //获得过滤器的类型
            Type filterType = Type.GetType( typeName );

            //创建日志过滤器
            object filter = Activator.CreateInstance( filterType );

            //用于保存配置节点处理器
            IConfigurationNodeHandler<ParseContext> handler;

            //用于保存处理的结果
            object instance = null;

            //用于保存属性信息
            System.Reflection.PropertyInfo propertyInfo = null;

            //用于保存属性
            PropertyValue property = null;

            //循环当前配置节点的所有子节点
            foreach (IConfigurationNode subNode in node.ChildNodes)
            {
                //获得配置节点处理器
                handler = context.GetNodeHandler( subNode );

                //判断是否为Null
                if (null == handler)
                {
                    //抛出异常
                    throw new NotSupportedException( string.Format( Properties.Resources.MSG_TAG_NOTSUPPORTED, subNode.Name ) );
                }

                //处理节点
                instance = handler.Porcess( subNode, context );

                //判断是否为属性
                if (subNode.Name == LogConfiguationTags.TAG_PROPERTY)
                {
                    //输出日志
                    if (LoggingContext.MetaLog.IsTraceEnabled)
                    {
                        LoggingContext.MetaLog.Trace( "开始解析日志过滤器'{0}'的属性", filter );
                    }

                    //转化为属性
                    property = (PropertyValue)instance;

                    //获得属性
                    propertyInfo = filterType.GetProperty( property.Name );

                    //设置属性值
                    propertyInfo.SetValue( filter, ObjectUtility.ConvertTo( property.Value, propertyInfo.PropertyType ), null );

                    //输出日志
                    if (LoggingContext.MetaLog.IsTraceEnabled)
                    {
                        LoggingContext.MetaLog.Trace( "日志过滤器'{0}'的属性'{1}'解析完毕", filter, property.Name );
                    }
                }
            }

            //输出日志
            if (LoggingContext.MetaLog.IsTraceEnabled)
            {
                LoggingContext.MetaLog.Trace( "日志过滤器'{0}'解析完毕", filter );
            }

            //返回创建的日志过滤器
            return filter;
        }
    }
}
